#include <iostream>
#include <stdio.h>
#include <memory>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <algorithm>
#include <sys/types.h>
#include <sys/stat.h>
#include <vector>
#include <dirent.h>
#include <map>
//Dir是否存在
int isExistDir(const char* dir)
{
    if(access(dir, 0) == -1)
        return -1;
    return 0;
}

//file是否存在
int isExistFile(const char* file)
{
    if(access(file,0) == -1)
        return -1;
    return 0;
}

int createDir(const char* dir)
{
    if(mkdir(dir,0775) == -1)
        return -1;
    return 0;
}

int createDirRecursion(const char* dir)
{
    std::string dirStr = std::string(dir);
    size_t p = dirStr.find("/");
    if(p != dirStr.npos){
        std::string tmp = dirStr.substr(0,p);
        //是否存在
        if(isExistDir(tmp.c_str()) != 0){
            if(createDir(tmp.c_str()) != 0)
                return -1;
        }
    }
    dirStr.substr(p);
    return 0;
}


/*get file time
// @function:  GetFileInfo
// @author:    wust_zc
// @date：    2022-03-04
// @brief    获取文件时间信息
// @param   strPath  文件路径
            iCreatTime  创建时间
            iModifyTime  改变时间
            iAccessTime  访问时间
// @return  int
//          0
//          1

*/
int GetFileInfo(std::string & strPath, int& iCreatTime, int& iModifyTime, int& iAccessTime)
{
    struct stat tmp;
    if(stat(strPath.c_str(), &tmp)!=0){
        return 0;
    }

    iCreatTime = static_cast<int>(tmp.st_ctime);
    iModifyTime = static_cast<int>(tmp.st_mtime);
    iAccessTime = static_cast<int>(tmp.st_atime);

    return 1;
}


void getFilenames(std::multimap<int, std::string, std::less<int>>& names, const std::string& dir, const std::string& type)
{
    DIR *dp;
    struct dirent *ptr;
 
    if ((dp = opendir(dir.c_str())) == NULL)
        return;
    while ((ptr = readdir(dp)) != NULL)
    {
        if (strcmp(ptr->d_name, ".") == 0 || strcmp(ptr->d_name, "..") == 0)    ///current dir OR parrent dir
            continue;
        else if (ptr->d_type == DT_REG)    ///file
        {
            std::string strname(ptr->d_name);
            if(strname.size() <= type.size())
                continue;
            if(strname.substr(strname.size()-type.size(), type.size()) != type){
                continue;
            }
 
            std::string strFileName(dir);
            strFileName.append("/").append(ptr->d_name);
            int a,b,c;
            GetFileInfo(strFileName,a,b,c);
            names.insert(std::make_pair(a,   strFileName.c_str()));

        }
        else if (ptr->d_type == DT_DIR)
        {
            continue;
        }
    }
    closedir(dp);
}


int main(int argc, char**argv)
{
    printf("************  rm log file!!!   **************\n");
    if(argc != 3){
        printf("The param is error\n");
        printf("For:[exe] [fullpath] [type]\n");
        printf("file dir\n");
        return 0;
    }
    printf("dir: %s\n", argv[1]);
    std::string dirPath = argv[1];
    std::string type = argv[2];

    // std::vector<std::string> files;

    std::multimap<int, std::string, std::less<int>> sorted_map;
    getFilenames(sorted_map, dirPath, type);

    for (auto pt:sorted_map)
    {
        printf("time: %d name: %s\n", pt.first, pt.second.c_str());
    }
    while(sorted_map.size() > 10){

        int key = sorted_map.begin()->first;
        std::string str = sorted_map.begin()->second;
        std::string cmd =  "rm -f " + str;
        printf("rm name  %s\n", str.c_str());
        int ret = system(cmd.c_str());
        if(ret !=0){
            printf("fail  %s\n", str.c_str());
            return 0;
        }
            
        sorted_map.clear();
        getFilenames(sorted_map, dirPath, type);
    }

    printf("************  rm log file!!!   **************\n");
    for (auto pt:sorted_map)
    {
        printf("time: %d name: %s\n", pt.first, pt.second.c_str());
    }
    return 0;
}
